package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.query.MPJQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.service.IInvoiceContractService;
import com.mdd.admin.validate.InvoiceContractCreateValidate;
import com.mdd.admin.validate.InvoiceContractUpdateValidate;
import com.mdd.admin.validate.InvoiceContractSearchValidate;
import com.mdd.admin.vo.InvoiceContractListedVo;
import com.mdd.admin.vo.InvoiceContractDetailVo;
import com.mdd.common.config.GlobalConfig;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.InvoiceContract;
import com.mdd.common.mapper.InvoiceContractMapper;
import com.mdd.common.util.ListUtils;
import com.mdd.common.util.TimeUtils;
import com.mdd.common.util.UrlUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 新建合同发票列实现类
 * @author LikeAdmin
 */
@Service
public class InvoiceContractServiceImpl implements IInvoiceContractService {
        
    @Resource
    InvoiceContractMapper invoiceContractMapper;

    /**
     * 新建合同发票列列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @return PageResult<InvoiceContractListedVo>
     */
    @Override
    public PageResult<InvoiceContractListedVo> list(PageValidate pageValidate, InvoiceContractSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<InvoiceContract> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        queryWrapper.orderByDesc("id");


        IPage<InvoiceContract> iPage = invoiceContractMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<InvoiceContractListedVo> list = new LinkedList<>();
        for(InvoiceContract item : iPage.getRecords()) {
            InvoiceContractListedVo vo = new InvoiceContractListedVo();
            BeanUtils.copyProperties(item, vo);
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 新建合同发票列详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return InvoiceContract
     */
    @Override
    public InvoiceContractDetailVo detail(Integer id) {
        InvoiceContract model = invoiceContractMapper.selectOne(
                new QueryWrapper<InvoiceContract>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        InvoiceContractDetailVo vo = new InvoiceContractDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 新建合同发票列新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(InvoiceContractCreateValidate createValidate) {
        InvoiceContract model = new InvoiceContract();
        invoiceContractMapper.insert(model);
    }

    /**
     * 新建合同发票列编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(InvoiceContractUpdateValidate updateValidate) {
        InvoiceContract model = invoiceContractMapper.selectOne(
                new QueryWrapper<InvoiceContract>()
                    .eq("id",  updateValidate.getId())
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setUserId(updateValidate.getUserId());
        model.setUserName(updateValidate.getUserName());
        model.setId(updateValidate.getId());
        model.setUpdateUserId(updateValidate.getUpdateUserId());
        model.setUpdateUserName(updateValidate.getUpdateUserName());
        model.setInvoiceTitle(updateValidate.getInvoiceTitle());
        model.setTotalPriceAll(updateValidate.getTotalPriceAll());
        model.setCreationTime(updateValidate.getCreationTime());
        model.setCreator(updateValidate.getCreator());
        model.setAuditor(updateValidate.getAuditor());
        model.setCurrentAuditNode(updateValidate.getCurrentAuditNode());
        model.setState(updateValidate.getState());
        invoiceContractMapper.updateById(model);
    }

    /**
     * 新建合同发票列删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        InvoiceContract model = invoiceContractMapper.selectOne(
                new QueryWrapper<InvoiceContract>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setIsDelete(1);
        model.setDeleteTime(System.currentTimeMillis() / 1000);
        invoiceContractMapper.updateById(model);
    }

}
